package com.example.listafacil.componentes;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.graphics.Paint;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.listafacil.BaseDados.TabelasCampos;

public abstract class ItemDeCompra extends LinearLayout {

	private EditText tx;
	private CheckBox cbx;
	private long id;
	private Map<String, String> valores;

	public ItemDeCompra(Context context, Map<String, String> valores, final boolean riscar) {
		super(context);
		setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		setOrientation(LinearLayout.HORIZONTAL);
		setGravity(Gravity.FILL_HORIZONTAL);

		if (valores == null) {
			valores = new HashMap<String, String>(3);
		}
		this.valores = valores;
		String valorID = valores.get(TabelasCampos.TabelaItem.ID_ITEM);
		if (valorID != null && !valorID.isEmpty()) {
			id = Long.parseLong(valorID);

		}
		cbx = new CheckBox(context);
		cbx.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		boolean comprado = false;
		String valorComprado = valores.get(TabelasCampos.TabelaItem.COMPRAR);
		if (valorComprado != null && !valorComprado.isEmpty()) {
			comprado = Boolean.parseBoolean(valorComprado);
		}
		cbx.setChecked(comprado);
		cbx.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LinearLayout grid = (LinearLayout) buttonView.getParent();
				EditText txt = (EditText) grid.getChildAt(1);
				if (riscar) {
					if (isChecked) {
						txt.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
						txt.setEnabled(false);
					} else {
						txt.setPaintFlags(0);
						txt.setEnabled(true);
					}
				}
				checkado();
			}
		});

		
		
		String valorDescricao = valores.get(TabelasCampos.TabelaItem.NOME);

		tx = new EditText(getContext());
		tx.setEms(10);
		tx.setInputType(InputType.TYPE_CLASS_TEXT);
		if (valorDescricao != null) {
			tx.setText(valorDescricao);
		}
		tx.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		tx.setSingleLine();
		tx.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(final View v, boolean hasFocus) {
				EditText tx = (EditText) v;
				if (hasFocus) {
					return;
				}
				ItemDeCompra linha = (ItemDeCompra) tx.getParent();
				caixadeTextoPerdeuOFoco(linha);
				
			}
		});

		Button btn = new Button(context);
		btn.setText("-");
		btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cliqueNoBotaoDeRemover();
				removeAllViews();
			}
		});
		addView(cbx);
		addView(tx);
		addView(btn);
	}
	public abstract void caixadeTextoPerdeuOFoco(ItemDeCompra linha);
	public abstract void checkado();
	public abstract void cliqueNoBotaoDeRemover();

	public Map<String, String> recuperarValores() {
		valores.put(TabelasCampos.TabelaItem.NOME, tx.getText().toString());
		valores.put(TabelasCampos.TabelaItem.COMPRAR, String.valueOf(cbx.isChecked()));
		valores.put(TabelasCampos.TabelaItem.ID_ITEM, String.valueOf(id));

		return valores;
	}

	public ContentValues recuperarValoresComoContentValues() {
		Map<String, String> recuperarValores = recuperarValores();
		Set<Entry<String, String>> entrySet = recuperarValores.entrySet();
		ContentValues valores = new ContentValues();
		for (Entry<String, String> entry : entrySet) {
			valores.put(entry.getKey(), entry.getValue());
		}
		return valores;
	}
}
